package com.zhaoyh.framework.taglib;

import com.zhaoyh.framework.code.model.PageBean;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

/**
 * @author 祥霸
 * @date 2019/3/4
 */
public class PageSpanAjaxTagHyper extends BodyTagSupport {

    private String beanName = null;

    private String ajaxFunction;

    private Integer i = 1;

    private String getPageNumberClick() {
        return "javascript:document.getElementById('pageNumber').value='" + this.i + "';" + this.ajaxFunction;
    }

    public String getAjaxFunction() {
        return ajaxFunction;
    }

    public void setAjaxFunction(String ajaxFunction) {
        this.ajaxFunction = ajaxFunction;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public int doEndTag() throws JspException {
        try {
            if (this.beanName == null || "".equals(this.beanName))
                this.beanName = "pageBean";
            PageBean pageBean = (PageBean) pageContext.getRequest().getAttribute(this.beanName);

            if (pageBean == null) {
                return super.doEndTag();
            }
            if (pageBean.getDataSum() == 0) {
                return super.doEndTag();
            }
            StringBuilder bfString = new StringBuilder("");
            // 隐藏属性
            bfString.append("<input type='hidden' id='pageNumber' name='pageNumber' value='" + pageBean.getPageNumber() + "'/>");

            bfString.append("<div class='row'>");

            bfString.append("<div class='col-sm-12 col-md-5' >");
            bfString.append("<div class='dataTables_info' id='datatable-buttons_info' role='status' aria-live='polite'>");

            // 当前页|总页数|总条数

            bfString.append("Showing " + ((pageBean.getPageNumber() - 1) * pageBean.getPageSize() + 1) + " to "+ (pageBean.getPageNumber()*pageBean.getPageSize()) +" of "+ pageBean.getDataSum() +" entries");

            bfString.append("</div>");
            bfString.append("</div>");

            bfString.append("<div class='col-sm-12 col-md-7'>");
            bfString.append("<div class='dataTables_paginate paging_simple_numbers' id='datatable-buttons_paginate'>");
            bfString.append("<ul class='pagination pagination-rounded'>");

            // 上一页
            if(pageBean.getPageNumber() - 1 <= 0) {
                bfString.append("<li class='paginate_button page-item previous disabled' id='datatable-buttons_previous'><a href='#' aria-controls='datatable-buttons' data-dt-idx='0' tabindex='0' class='page-link'><i class='mdi mdi-chevron-left'></i></a></li>");
            } else {
                this.i = pageBean.getPageNumber() - 1;
                bfString.append("<li class=\"paginate_button page-item previous\" id=\"datatable-buttons_previous\"><a href=\""+ getPageNumberClick() +"\" aria-controls=\"datatable-buttons\" data-dt-idx=\"0\" tabindex=\"0\" class=\"page-link\"><i class=\"mdi mdi-chevron-left\"></i></a></li>");
            }

            // 第一页
            if (pageBean.getPageNumber() != 1) {
                this.i = 1;
                bfString.append("<li class=\"paginate_button page-item \"><a href=\""+ getPageNumberClick() +"\" aria-controls=\"datatable-buttons\" data-dt-idx=\"1\" tabindex=\"0\" class=\"page-link\">1</a></li>");
            }
            if (pageBean.getPageNumber() > 4) {
                bfString.append("...&nbsp;&nbsp;&nbsp;");
            }

            // 当前页前2个页码
            for (int i = pageBean.getPageNumber() - 2, c = 2; c > 0; i++, c--) {
                if (i < 2) continue;
                this.i = i;
                bfString.append("<li class=\"paginate_button page-item \"><a href=\""+ getPageNumberClick() +"\" aria-controls=\"datatable-buttons\" data-dt-idx=\"" + i + "\" tabindex=\"0\" class=\"page-link\">" + i +"</a></li>");
            }
            // 当前页
            bfString.append("<li class=\"paginate_button page-item active\"><a href=\"#\" aria-controls=\"datatable-buttons\" data-dt-idx=\"" + pageBean.getPageNumber() + "\" tabindex=\"0\" class=\"page-link\">" + pageBean.getPageNumber() + "</a></li>");

            // 当前页后2个页码
            for (int i = pageBean.getPageNumber() + 1, c = 2; i < pageBean.getPageSum() && c > 0; i++, c--) {
                this.i = i;
                bfString.append("<li class=\"paginate_button page-item \"><a href=\""+ getPageNumberClick() +"\" aria-controls=\"datatable-buttons\" data-dt-idx=\"" + i + "\" tabindex=\"0\" class=\"page-link\">" + i + "</a></li>");
            }

            // ...
            if (pageBean.getPageNumber() + 3 < pageBean.getPageSum()) {
                bfString.append("...&nbsp;&nbsp;&nbsp;");
            }

            // 最后一页
            if (pageBean.getPageNumber() != pageBean.getPageSum()) {
                this.i = pageBean.getPageSum();
                bfString.append("<li class=\"paginate_button page-item \"><a href=\""+ getPageNumberClick() +"\" aria-controls=\"datatable-buttons\" data-dt-idx=\"" + pageBean.getPageSum() + "\" tabindex=\"0\" class=\"page-link\">" + pageBean.getPageSum() + "</a></li>");
            }

            // 下一页
            if (pageBean.getPageNumber() >= pageBean.getPageSum()) {
                bfString.append("<li class=\"paginate_button page-item next disabled\" id=\"datatable-buttons_next\"><a href=\"#\" aria-controls=\"datatable-buttons\" data-dt-idx=\""+ (pageBean.getPageSum() + 1) +"\" tabindex=\"0\" class=\"page-link\"><i class=\"mdi mdi-chevron-right\"></i></a></li>");
            } else {
                this.i = pageBean.getPageNumber() + 1;
                bfString.append("<li class=\"paginate_button page-item next\" id=\"datatable-buttons_next\"><a href=\""+ getPageNumberClick() +"\" aria-controls=\"datatable-buttons\" data-dt-idx=\""+ (pageBean.getPageSum() + 1) +"\" tabindex=\"0\" class=\"page-link\"><i class=\"mdi mdi-chevron-right\"></i></a></li>");
            }

            bfString.append("</ul>");

            bfString.append("</div");
            bfString.append("</div>");

            bfString.append("</div>");
            bfString.append("</div>");


            pageContext.getOut().print(bfString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.doEndTag();
    }

}
